package com.gylang.auth.acpect;

/**
 * @author gylang,
 * @data 2019/11/24 11:16,
 * @DESC
 */

import com.gylang.auth.message.LogMessage;
import com.gylang.auth.service.SysLogService;
import com.gylang.auth.DTO.LoginInfoDTO;
import com.gylang.auth.utils.IPUtils;
import com.gylang.auth.utils.JsonUtils;
import com.gylang.auth.utils.LoginUtils;
import com.gylang.common.model.log.SysLogMq;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 系统日志，切面处理类，记录日志
 * @date Jan 19, 2019
 */
@Aspect
@Component
public class SysLogAspect {

    @Autowired
    private LogMessage logMessage;
    @Pointcut("execution(* com.gylang.auth.controller.*.*(..))" +
            "&& !execution(public * com.gylang.auth.controller.SysLoginController.*(..))" +
    "&& !execution(public * com.gylang.auth.controller.TipsController.*(..))")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        // 执行方法
        Object result = point.proceed();
        // 执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        // 保存日志
        saveSysLog(point, time);
        return result;
    }

    private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {

        LoginInfoDTO loginInfoDTO = LoginUtils.getLoginInfo();
        String userName ="" ;
        if (null != loginInfoDTO) {
            userName = loginInfoDTO.getName();
        }
        if(joinPoint.getTarget() instanceof SysLogService) {
            return ;
        }
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        SysLogMq sysLogMq = new SysLogMq();

//		Method method = signature.getMethod();
//		com.louis.merak.admin.annotation.SysLogMq syslogAnno = method.getAnnotation(com.louis.merak.admin.annotation.SysLogMq.class);
//		if(syslogAnno != null){
//			//注解上的描述
//			sysLog.setOperation(syslogAnno.value());
//		}

        // 请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLogMq.setMethod(className + "." + methodName + "()");

        // 请求的参数
        Object[] args = joinPoint.getArgs();
        try{
            String params = JsonUtils.obj2Str(args[0]);
            if(params.length() > 200) {
                params = params.substring(0, 200) + "...";
            }
            sysLogMq.setParams(params);
        } catch (Exception e){
        }

        // 获取request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

//        HttpServletRequest request = HttpUtils.getHttpServletRequest();
        // 设置IP地址
        sysLogMq.setIp(IPUtils.getIpAddr(request));

        // 用户名
        sysLogMq.setUserName(userName);

        // 执行时长(毫秒)
        sysLogMq.setTime(time);

        // 保存系统日志
       logMessage.sysLog(sysLogMq);
    }

}

